package com.zm.zmgz.service;

import com.zm.zmgz.annotationzm.EmployeeFieldName;
import com.zm.zmgz.constant.ConstantInterface;
import com.zm.zmgz.dao.ShowEmployeeBeanMapper;
import com.zm.zmgz.pojo.*;
import com.zm.zmgz.utils.EmployeeUtils;
import com.zm.zmgz.utils.UniversalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

@Service
public class ShowEmployeeService { // 提供ShowEmployeeZm表相关服务

    @Autowired
    ShowEmployeeBeanMapper showEmployeeBeanMapper;

    public  Map<String, List<String>> getDisplayModes(String identificationNumber) { // 传入身份证,返回显示模式信息
        ShowEmployeeBeanExample example = new ShowEmployeeBeanExample();
        ShowEmployeeBeanExample.Criteria criteria = example.createCriteria();
        criteria.andIdentificationnumberEqualTo(identificationNumber);
        List<ShowEmployeeBean> list = showEmployeeBeanMapper.selectByExample(example);
        if(list.size() == 0) { // list.size为0,表示该用户的showEmployee记录还没有初始化,需要初始化.完整为全部显示,精简为空,隐藏为全部隐藏
            List<String> attributesList =  EmployeeUtils.getAttributes(EmployeeBean.class); // 员工对象的属性列表;由于element table有一个bug,第一个生成的列,会出现在最后一列,所以在后端,我会把最后一个属性发在第一位.
            String s1 = attributesList.get(attributesList.size() - 1); // 获取最后一个元素的值
            attributesList.remove(attributesList.size()-1); // 删除最后一个元素.
            attributesList.add(0,s1); // 把最后一个元素添加到首位
            String displayOrder = "";
            for (String s : attributesList) { // 拼接displayOrder字段值
                displayOrder += s + "/";
            }
            showEmployeeBeanMapper.insert(new ShowEmployeeBean(identificationNumber,displayOrder, ConstantInterface.FULLMODEWORKSNUMBER)); // 完整模式有效;插入一条默认值
            showEmployeeBeanMapper.insert(new ShowEmployeeBean(identificationNumber,"",ConstantInterface.THINMODEISINVALIDNUMBER)); // 精简模式无效
            showEmployeeBeanMapper.insert(new ShowEmployeeBean(identificationNumber,displayOrder,ConstantInterface.HIDDENITEMNUMBER)); // 隐藏项
            list = showEmployeeBeanMapper.selectByExample(example); // 刷新
        }
        Map<String, List<String>> displayEmployees = new HashMap<>();
        for (ShowEmployeeBean bean : list) { // 添加完整模式,精简模式,隐藏项数据到返回值
            List<String> displayMode = new ArrayList<>(); // 存储显示模式数组
            String[] strings = bean.getDisplayorder().split("/");
            for (String s : strings) { // 添加数据到list中
                displayMode.add(s);
            }
            switch (bean.getMark()){ // 根据标记,使用不同的键值
                case ConstantInterface.FULLMODEWORKSNUMBER :
                    displayEmployees.put(ConstantInterface.FULLMODEWORKS,displayMode);
                    break;
                case ConstantInterface.FULLMODEISINVALIDNUMBER :
                    displayEmployees.put(ConstantInterface.FULLMODEISINVALID,displayMode);
                    break;
                case ConstantInterface.THINMODEWORKSNUMBER :
                    displayEmployees.put(ConstantInterface.THINMODEWORKS,displayMode);
                    break;
                case ConstantInterface.THINMODEISINVALIDNUMBER :
                    displayEmployees.put(ConstantInterface.THINMODEISINVALID,displayMode);
                    break;
                case ConstantInterface.HIDDENITEMNUMBER :
                    displayEmployees.put(ConstantInterface.HIDDENITEM,displayMode);
                    break;
            }
        }
        return displayEmployees;
    }


    /*
    * 功能:获取显示模式信息
    * 从showemployeezm表获取showemployeezmBean集合,根据mark值来处理集合.
    * */
    public Map<String, List<Map<String, String>>> getDisployData(String identificationnumber) { // 获取显示模式信息
       ShowEmployeeBeanExample example = new ShowEmployeeBeanExample(); // 创建例子
        ShowEmployeeBeanExample.Criteria criteria = example.createCriteria(); // 创建标准
        criteria.andIdentificationnumberEqualTo(identificationnumber); // 标准:身份证值相等
        List<ShowEmployeeBean> list = showEmployeeBeanMapper.selectByExample(example); // 获取ShowEmployeeBean对象集合
        Map<String, List<Map<String, String>>> map = new HashMap<String, List<Map<String, String>>>(); // 创建map集合,存储3种显示模式信息
        for (ShowEmployeeBean showEmployeeBean : list) { // 根据ShowEmployeeBean的mark值来处理集合;循环结束3种显示模式数据填充完毕
            List<Map<String,String>> mapList = new ArrayList<>(); // 存储一条显示模式数据
            String disployName = null; // map的key值
            switch (showEmployeeBean.getMark().toString().charAt(0)){ // 确定显示的模式
                case '1' :
                    disployName = "fullModeWorksList";
                    break;
                case '2' :
                    disployName = "thinModeIsNvalidList";
                    break;
                case '3' :
                    disployName = "hiddeItemList";
                    break;
            }
            if(!Objects.isNull(showEmployeeBean.getDisplayorder())) { // 如果显示内容不为空
                String[] strings = showEmployeeBean.getDisplayorder().split("/");
                if(strings.length>0) { // 排除空串
                    for (String s : strings) { // 添加数据;循环结束一个maplist集合就填充完毕
                        if (s.length() > 0) { // 排除空字符
                            String name = null; // stringMap的key
                            Map<String, String> stringMap = new HashMap<>(); // 存储一个属性名
                            try {
                                Field field = EmployeeBean.class.getDeclaredField(s);
                                field.setAccessible(true);
                                EmployeeFieldName fieldAnnotation = field.getAnnotation(EmployeeFieldName.class);
                                name = fieldAnnotation.value(); // stringMap的key
                            } catch (NoSuchFieldException e) {
                                e.printStackTrace();
                            }
                            stringMap.put("name", name); // 往stringMap添加一个键值对
                            stringMap.put("fieldName",s);
                            mapList.add(stringMap); // 把stringMap添加到mapList集合中
                        }
                    }
                }
                if(mapList.size()>0) { // 反向抵消element-ui table 显示bug
                    Map<String, String> stringMap = mapList.get(0);
                    mapList.remove(0);
                    mapList.add(stringMap);
                }
            }
            map.put(disployName,mapList); // 一个显示模式添加完毕
        }

        return map;
    }


    public int setDisployData(String identificationnumber, DisployDataBean disployDataBean) { // 设置显示模式信息
        ShowEmployeeBeanExample example = new ShowEmployeeBeanExample(); // 创建例子
        ShowEmployeeBeanExample.Criteria criteria = example.createCriteria(); // 创建标准
        criteria.andIdentificationnumberEqualTo(identificationnumber); // 标准:身份证值相等
        List<ShowEmployeeBean> list = showEmployeeBeanMapper.selectByExample(example); // 获取ShowEmployeeBean对象集合
        List<FullModeWorksListBean> fullModeWorksList = disployDataBean.getFullModeWorksList();
        List<HiddeItemListBean> hiddeItemList = disployDataBean.getHiddeItemList();
        List<ThinModeIsNvalidListBean> thinModeIsNvalidList = disployDataBean.getThinModeIsNvalidList();
        if(fullModeWorksList.size()>0){ // 抵消element-ui table显示bug
            FullModeWorksListBean fullModeWorksListBean = fullModeWorksList.get(fullModeWorksList.size() - 1);// 获取最后一个元素的值
            fullModeWorksList.remove(fullModeWorksList.size()-1); // 删除最后一个元素
            fullModeWorksList.add(0,fullModeWorksListBean); // 把最后一个元素添加到首位
        }
        if(hiddeItemList.size()>0){ // 抵消element-ui table显示bug
            HiddeItemListBean hiddeItemListBean = hiddeItemList.get(hiddeItemList.size() - 1);// 获取最后一个元素的值
            hiddeItemList.remove(hiddeItemList.size()-1); // 删除最后一个元素
            hiddeItemList.add(0,hiddeItemListBean); // 把最后一个元素添加到首位
        }
        if(thinModeIsNvalidList.size()>0){ // 抵消element-ui table显示bug
            ThinModeIsNvalidListBean thinModeIsNvalidListBean = thinModeIsNvalidList.get(thinModeIsNvalidList.size() - 1);// 获取最后一个元素的值
            thinModeIsNvalidList.remove(thinModeIsNvalidList.size()-1); // 删除最后一个元素
            thinModeIsNvalidList.add(0,thinModeIsNvalidListBean); // 把最后一个元素添加到首位
        }
        String fullModeWorksListDisplayOrder = "";
        String hiddeItemListDisplayOrder = "";
        String thinModeIsNvalidListDisplayOrder = "";
        for (FullModeWorksListBean fullModeWorksListBean : fullModeWorksList) { // fullModeWorksList解析为displayOrder字符串
            fullModeWorksListDisplayOrder += fullModeWorksListBean.getFieldName() + "/";
        }
        for (HiddeItemListBean hiddeItemListBean : hiddeItemList) { // hiddeItemList解析为displayOrder字符串
            hiddeItemListDisplayOrder += hiddeItemListBean.getFieldName() + "/";
        }
        for (ThinModeIsNvalidListBean thinModeIsNvalidListBean : thinModeIsNvalidList) { //thinModeIsNvalidList解析为displayOrder字符串
            thinModeIsNvalidListDisplayOrder += thinModeIsNvalidListBean.getFieldName() + "/";

        }
        for (ShowEmployeeBean showEmployeeBean : list) { // 把displayOrder属性重新赋值
            switch (showEmployeeBean.getMark().toString().charAt(0)){ // 确定显示的模式
                case '1' :
                    showEmployeeBean.setDisplayorder(fullModeWorksListDisplayOrder);
                    break;
                case '2' :
                    showEmployeeBean.setDisplayorder(thinModeIsNvalidListDisplayOrder);
                    break;
                case '3' :
                    showEmployeeBean.setDisplayorder(hiddeItemListDisplayOrder);
                    break;
            }
        }
        int count = 0;
        for (ShowEmployeeBean showEmployeeBean : list) { // 把3个showEmployeeBean更新到数据库
             count += showEmployeeBeanMapper.updateByPrimaryKey(showEmployeeBean);
        }
        return count;
    }
}
